<template>
  <div>
    <el-dialog :visible="showDialog" v-bind="$attrs" :title="form.id ? '编辑部门' : '新增部门'" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入部门名称" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入部门编码" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" placeholder="请选择部门负责人" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in managerOptions"
              :key="index"
              :label="item.username"
              :value="item.username"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            type="textarea"
            placeholder="请输入部门介绍"
            :autosize="{minRows: 4, maxRows: 4}"
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>
        <el-form-item prop="id"></el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getEmployeesSimpleAPI, getDepartmentsAPI, updateDepartmentsAPI } from '@/api'
import { addDepartmentsAPI } from '@/api'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    node: { // 新增-父部门 // 编辑-当前部门
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      // 统计部门中禁止出现重复部门
      // 分析有哪些数据，和数据操作
      // 府部门，this.node
      // 所有部门，通过接口，拿到所有数据
      // 统计部门，添加那些部门的兄弟部门，赛选部门pid等于父部门id
      // 不允许重复，find筛选
      // console.log(this.node)

      const { depts } = await getDepartmentsAPI() // 拿到所有部门
      let brothers
      if (this.form.id) {
        // 编辑
        brothers = depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id) // 判断输入的值和当前所有部门有没有重复
      } else {
        // 新增
        brothers = depts.filter(item => item.pid === this.node.id) // 拿到传来部门所有的的亲儿子部
      }
      if (brothers.find(t => t.name === value)) { // 判断输入的值和当前所有儿子部门有没有重复
        callback('统计部门存在重复名称')
      } else {
        callback()
      }
      // console.log(depts)
    }
    const checkCode = async(rule, value, callback) => {
      // 部门编码整个模块不允许重复
      // 分析有哪些数据，和数据进行对比
      // 部门编码，当前表单新增部门编码this.form.code / value
      // 整个模块，所有的其他部门编码，通过接口getEmployeesSimpleAPI方法获取部门编码
      // 不允许当前输入编码和部门编码有重复
      // 通过find进行查找或者filter
      let { depts } = await getDepartmentsAPI()
      if (this.form.id) {
        // 编辑
        depts = depts.filter(t => t.id !== this.form.id)
      }
      // 新增
      depts = depts.find(t => t.code === value) ? callback('存在重复编码') : callback()
    }
    return {
      form: {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined,
        id: null
      },
      rules: {
        name: [{
          required: true,
          message: '请输入部门名称',
          trigger: 'blur'
        }, {
          min: 1, max: 50, message: '1-50个字符', trigger: 'blur'
        }, {
          validator: checkName,
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入部门编码',
          trigger: 'blur'
        }, {
          min: 1, max: 50, message: '1-50个字符', trigger: 'blur'
        }, {
          validator: checkCode,
          trigger: 'blur'
        }],
        manager: [{
          required: true,
          message: '请选择部门负责人',
          trigger: 'change'
        }],
        introduce: [{
          required: true,
          message: '请输入部门介绍',
          trigger: 'blur'
        }, {
          min: 1, max: 300, message: '1-300个字符', trigger: 'blur'
        }]
      },
      managerOptions: []
    }
  },
  computed: {},
  watch: {},
  async created() {
    this.managerOptions = await getEmployeesSimpleAPI()
  },
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['formRef'].resetFields()
      this.form = {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined,
        id: null
      }
      this.$emit('update:showDialog', false)
    },
    //  确定
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 发送请求
        if (this.form.id) {
          // 编辑
          await updateDepartmentsAPI(this.form)
        } else {
          // 新增
          await addDepartmentsAPI({ ...this.form, pid: this.node.id })
        }
        this.$message.success('操作成功 ')
        this.$emit('success')
        this.onClose()
      })
    }
  }
}

</script>
<style>
</style>
